package work.timejay.user.utils;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

/**
 * @className:WeChatUtils.java
 * @author:王文杰
 * @version:1.0.0
 * @description:TODO
 * @createTime 2022年04月10日 19:27:00
 */
@Slf4j
public class WeChatUtils {
    /**
     *
     */
    private static final String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx24c3076899066c8c&secret=8e7d997f8309529768f5ffd64c55b0e9";
    private static final String qrCodeUrl = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=";

    /**
     * 公众号获取Accesstoken
     */
    public static String getAccessToken() throws IOException {
        HttpClient httpClient = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet(url);
        //执行
        HttpResponse response = httpClient.execute(httpGet);
        String result = EntityUtils.toString(response.getEntity());
        log.info("accessToken--->{}", result);
        return result;
    }

    /**
     * 微信开放平台测试账号
     *
     * @return
     * @throws IOException
     */
    public static String getTestAccessToken() throws IOException {
        HttpClient httpClient = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx24c3076899066c8c&secret=6c183f560ee60765cc303e7d121d18eb");
        //执行
        HttpResponse response = httpClient.execute(httpGet);
        String result = EntityUtils.toString(response.getEntity());
        log.info("accessToken--->{}", result);
        return result;
    }

    /**
     * 生成临时二维码
     */
    public static String getQRCode(String token, Map<String, Object> mapDTO) throws IOException {
        HttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(qrCodeUrl + token);
        httpPost.setEntity(new StringEntity(JSONUtil.toJsonStr(mapDTO)));
        log.info("mapDTO--->{}", JSONUtil.toJsonStr(mapDTO));
        //执行
        HttpResponse response = httpClient.execute(httpPost);
        //获取响应结果
        String result = EntityUtils.toString(response.getEntity());
        return result;
    }

    /**
     * 获取生成的二维码 用户扫码后，可以触发事件推送，进行扫码关注登录
     */
    public static InputStream getFile(String code) throws IOException {
        HttpClient httpClient = HttpClients.createDefault();
        HttpGet get = new HttpGet("https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket="+code);
        //执行
        HttpResponse response = httpClient.execute(get);
        //获取响应结果
        return response.getEntity().getContent();
    }
}
